/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bibp2pdesk;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

/**
 *
 * @author daniel
 */
public class CatalogClient {
    
    private Socket socket = null;
    private OutputStream out = null;
    private DataOutputStream dos = null;
    private InputStream in = null;
    private DataInputStream dis = null;

    public CatalogClient(String ip) throws Exception {

        socket = new Socket(ip.split(":")[0], Integer.decode(ip.split(":")[1]));
        out = socket.getOutputStream();
        dos = new DataOutputStream(out);
        in = socket.getInputStream();
        dis = new DataInputStream(in);

    }

    public void sendData(int type, byte[] data) throws IOException {

        dos.writeInt(type);
        dos.writeInt(data.length);
        dos.write(data,0,data.length);

    }

    public int recvType() throws IOException {

        return dis.readInt();

    }

    public byte[] recvData() throws IOException {

        int length;
        length = dis.readInt();
        byte data[] = new byte[length];
        dis.readFully(data);

        return data;

    }

}
